import json

from django.http import JsonResponse
from django.shortcuts import render

# Create your views here.
from django.views import View

from books.models import BookInfo, HeroInfo
from books.serializers import BookSerialzier, HeroSerialzier


class BooksView(View):
    def post(self,request):
        """
                   保存图书
               :param request:
               :return:
                """
        # 1、前端传递保存图书的数据 json接受
        json_data = request.body.decode()
        dict_data = json.loads(json_data)
        btitle = dict_data.get('btitle')
        bpub_date = dict_data.get('bpub_date')
        bread = dict_data.get('bread')
        # 2、验证数据
        if not all([btitle, bpub_date]):
            return JsonResponse({'code': '400'}, status=400)

        # 3、保存数据
        book = BookInfo.objects.create(btitle=btitle, bpub_date=bpub_date)
        # 4、返回结果
        #使用徐略后期进行序列化操作
        #初始化生成序列化器队形   将需要进行数据构建的对象传递到序列号其中
        ser=BookSerialzier(book)
        #通脱序列化器对象的data方法获取构造好的返回结果
        return JsonResponse(ser.data)
        # return JsonResponse({
        #     'id': book.id,
        #     'btitle': book.btitle,
        #     'bpub_date': book.bpub_date,
        #     'bread': book.bread
        # })

    def get(self, request):
        """
                获取所有图书
               :param request:
               :return:
        """
        books = BookInfo.objects.all()
        # book_list = []
        # for book in books:
        #     book_list.append({
        #         'id': book.id,
        #         'btitle': book.btitle,
        #         'bpub_date': book.bpub_date,
        #         'bread': book.bread
        #     })
        #返回多个数据时，需要额外增加many=True参数
        ser=BookSerialzier(books,many=True)

        return JsonResponse({'books': ser.data})


class BookView(View):
    def delete(self,request,id):
        """
                     删除一本图书
                 :param request:
                 :param pk: 图书id
                 :return:
                 """
        try:
            book = BookInfo.objects.get(id=id)
        except:
            return JsonResponse({'code': '400'}, status=400)

        # 逻辑删除
        book.is_delete = True
        book.save()
        # 物理删除
        # book.delete()
        return JsonResponse({})

    def put(self, request, id):
        """
            更新一本图书
        :param request:
        :param pk: 图书id
        :return:
        """
        # 查询更新的图书
        try:
            book = BookInfo.objects.get(id=id)
        except:
            return JsonResponse({'code': '400', 'message': '数据不存在'}, status=400)
        # 获取更新的字段
        json_data = request.body.decode()
        dict_data = json.loads(json_data)
        btitle = dict_data.get('btitle')
        bpub_date = dict_data.get('bpub_date')
        bread = dict_data.get('bread')

        # if btitle:
        #     book.btitle=btitle
        # if bpub_date:
        #     book.bpub_date=bpub_date
        # if bread:
        #     book.bread=bread
        # book.save()
        BookInfo.objects.filter(id=id).update(**dict_data)
        book = BookInfo.objects.get(id=id)

        return JsonResponse({
            'id': book.id,
            'btitle': book.btitle,
            'bpub_date': book.bpub_date
        })

    def get(self, request, id):
        """
            获取一本图书
        :param request:
        :param pk:  图书id
        :return:
        """
        try:
            book = BookInfo.objects.get(id=id)
        except:
            return JsonResponse({'code': '400', 'message': '数据不存在'}, status=400)

        return JsonResponse({
            'id': book.id,
            'btitle': book.btitle,
            'bpub_date': book.bpub_date,
            'bread': book.bread
        })




class HerosView(View):

    def get(self, request):
        """
                获取所有英雄
        """
        heros = HeroInfo.objects.all()

        #返回多个数据时，需要额外增加many=True参数
        ser=HeroSerialzier(heros,many=True)

        return JsonResponse({'heros': ser.data})

class HeroView(View):
    def get(self, request, id):
        """
            获取一本英雄
        :param request:
        :param pk:  图书id
        """
        try:
            hero = HeroInfo.objects.get(id=id)
        except:
            return JsonResponse({'code': '400', 'message': '数据不存在'}, status=400)
        ser = HeroSerialzier(hero)
        # 通过序列化器对象的data方法获取构造好的返回结果
        return JsonResponse(ser.data)